package bddaccess;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Iterator;

import modex.Document;
import modex.Paragraphe;

public class ParagrapheDAO extends DAO<Paragraphe> {

	@Override
	public Paragraphe create(Paragraphe obj) {
		try {
			int id = 0;
			PreparedStatement prepare = this.connect
			                            	.prepareStatement(
		                            			"INSERT INTO Paragraphe (idParagraphe, xPath, idDocument) VALUES(?, ?, ?)",
		                            			Statement.RETURN_GENERATED_KEYS
			                            	);
			prepare.setInt(1, id);
			prepare.setString(2, obj.getxPath());
			prepare.setInt(3, obj.getIdDocument());
			
			prepare.executeUpdate();
			
			ResultSet rs = prepare.getGeneratedKeys();
			if (rs.next()){
			    id=rs.getInt(1);
			}
			obj = this.find(id);

	    } catch (SQLException e) {
	            e.printStackTrace();
	    }
	    return obj;
	}

	public void createMultiple(ArrayList<Paragraphe> obj, int lot) {
		
		StringBuilder r = new StringBuilder();
		
		Iterator<Paragraphe> it = obj.iterator();
		
		while(it.hasNext()){			
			r.append("INSERT INTO Paragraphe (idParagraphe, xPath, idDocument) VALUES ");
			Paragraphe p = it.next();
			r.append("(")
				.append(p.getIdParagraphe())
					.append(",'")
						.append(p.getxPath())
							.append("',")
								.append(p.getIdDocument())
									.append(")");
			for(int i=1; i<lot && it.hasNext(); i++){
				p = it.next();
				r.append(",");
				r.append("(")
				.append(p.getIdParagraphe())
					.append(",'")
						.append(p.getxPath())
							.append("',")
								.append(p.getIdDocument())
									.append(")");
			}			
		}	
		
		try {
			this.connect.createStatement().executeUpdate(r.toString());
		} catch (SQLException e) {
			e.printStackTrace();
		}		
	}

	@Override
	public void delete(Paragraphe obj) {
		try {			
            this.connect
               	.createStatement()
               	.executeUpdate(
               			"DELETE FROM Paragraphe WHERE idParagraphe = " + obj.getIdParagraphe()
                );		
	    } catch (SQLException e) {
	            e.printStackTrace();
	    }
	}

	@Override
	public Paragraphe find(int id) {
		Paragraphe paragraphe = new Paragraphe();               
        
        try {
	        ResultSet result = this.connect
	                               .createStatement()
	                               .executeQuery(
	                                    "select * from Paragraphe "+
	                                    " where idParagraphe = "+ id
	                               );

            if(result.first()){
            	paragraphe = new Paragraphe(id, result.getString("xPath"), result.getInt("idDocument"));
            }
            else{ paragraphe = null; }
        } catch (SQLException e) {
                e.printStackTrace();
        }
        return paragraphe;
	}

	public Document findDocument(int idP) {
		Document document = new Document();
		
		try {
	        ResultSet result = this.connect
	                               .createStatement()
	                               .executeQuery(
	                                    "SELECT Document.*" +
	                                    " FROM Document, Paragraphe "+
	                                    " WHERE Document.idDocument = Paragraphe.idDocument" +
	                                    " AND Paragraphe.idParagraphe = " + idP
	                               );

            if(result.first()){
            	document.setIdDocument(result.getInt("idDocument"));
            	document.setNomDocument(result.getString("nomDocument"));
            }
            else{ return null; }
        } catch (SQLException e) {
                e.printStackTrace();
        }       
        
		return document;
	}
	
	@Override
	public Paragraphe update(Paragraphe obj) {
		try {			
            this .connect	
                 .createStatement()
                 .executeUpdate(
                	"UPDATE Paragraphe SET xPath = '" + obj.getxPath() + "',"+
                	" idDocument = " + obj.getIdDocument() +
                	" WHERE idParagraphe = " + obj.getIdParagraphe()
                 );
		
            obj = this.find(obj.getIdParagraphe());
	    } catch (SQLException e) {
	            e.printStackTrace();
	    }	    
	    return obj;
	}

	public int nbParagraphes() {
		int nbPara = 0;
		
		return nbPara;
	}

	// TODO : Remove si non utilisé
	public int count(){
		int nbPara = 0;
		 try {
		        ResultSet result = this.connect
		                               .createStatement()
		                               .executeQuery(
		                            		   "SELECT COUNT(p.idParagraphe) from Paragraphe p"
		                               );

	            if(result.first()){
	            	nbPara = result.getInt("COUNT(p.idParagraphe)");
	            }	            
	        } catch (SQLException e) {
	                e.printStackTrace();
	        }
		return nbPara;
	}
	
	public Paragraphe findByXpathAndDocId(String xpath, int docID){
		Paragraphe paragraphe = new Paragraphe();               
        
        try {        	
	        ResultSet result = this.connect
	                               .createStatement()
	                               .executeQuery(
	                                    "select * from Paragraphe "+
	                                    " where xPath = '"+ xpath + "' and idDocument = " + docID
	                               );

            if(result.first()){
            	paragraphe = new Paragraphe(result.getInt("idParagraphe"), xpath, docID);
            }
            else{ paragraphe = null; }
        } catch (SQLException e) {
                e.printStackTrace();
        }
        return paragraphe;
	}
}
